<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台公用控制器。
 *
 **/

namespace Home\Controller;

use Think\Controller;

class ComController extends Controller
{

    public function _initialize()
    {
        C(setting());
		//用户登录信息
    }

    /**
     * 如果没有session的话就去跳转认证,然后再赋值session
     */
    public function is_session()
    {
//        session_destroy();
        //如果session['user']为空
        if (empty($_SESSION['user'])) {
//            echo 111;die;
            $res = $this->getsession($_GET['open_id_pre'],$_GET['session_id']);
//            dump($res);die;
            //如果session表中有数据,就赋值给session['user']
            if ($res['result'] == 1) {
                $_SESSION['user'] = $res['data'];
                //赋值logo和统计id 和赋值$user
                $this->assign('user',$_SESSION['user']);
                $open_id_pre= $_SESSION['user']['open_id_pre'];
                //dump($_SESSION);die;
                $platform = D('platform')->field('logo,tongji_id,platform_name')->where("open_id_pre='$open_id_pre'")->find();
                //dump($platform);die;
                $this->assign('platform',$platform);
                //跳转认证
            }else {
                 $function = ACTION_NAME;
//                 echo $function;die;
                 if ($function == 'ajaxgetchapter') {
                     //dump('111');die;
                     $data['is_identity'] = 1;
                     $map['show_domain'] = $_SERVER['HTTP_HOST'];
                     preg_match_all('/\d+/', $_SERVER['REQUEST_URI'], $chapter_id_arr);
                     $chapter_id = $chapter_id_arr[0][1];
                     $order_set_arr = D('novel_chapter')->field('order_set,novel_id')->where('id=' . $chapter_id)->find();
                     $novel_id = $order_set_arr['novel_id'];
                     $data['redirect_url'] = "/Home/novel/show/novel_id/$novel_id/id/" . $chapter_id;
                     $domain_arr = D('domain')->where($map)->find();
                     $domain = $domain_arr['set_domain'];

                     $data['identity_url'] = "http://".$domain."/Home/Identify/identify";
                     //dump($data);die;
                     echo $this->ajaxReturn($data);
                     die;
                 }
				
                //如果没有数据,就跳转到认证

                $redirect = $_SERVER['REQUEST_URI'];
				$redirect = str_replace('&','/',$redirect);
				$redirect = str_replace('=','/',$redirect);
				$redirect = str_replace('?','/',$redirect);

                //先判断是否是显示域名
                $map['show_domain'] = $_SERVER['HTTP_HOST'];
//                echo $_SERVER['HTTP_HOST'];die;
                $domain = D('domain')->where($map)->find();
//                echo D('domain')->getLastSql();
//                die();
                //跳认证
				$url = "http://" . $domain['set_domain'] . "/Home/Identify/identify?redirect=" . $redirect;
//				echo $url;die;
                header("Location: " . $url);
                die;
            }

		}else{
            //不为空就赋值统计id和logo图片 和赋值$user
            $this->assign('user',$_SESSION['user']);
            $open_id_pre= $_SESSION['user']['open_id_pre'];
            $platform = D('platform')->field('logo,tongji_id,platform_name')->where("open_id_pre='$open_id_pre'")->find();
            $this->assign('platform',$platform);
        }
    }
	
	/**
	 * 数据库获取session
	 */
	private function getsession($open_id_pre = '', $session_id = ''){
		if(!empty($open_id_pre) && !empty($session_id)) {
        //如果有open_id_pre和session_id(绑定域名的session_id)的时候，就去做一次认证，第二次在走这里就属于非法操作。
			$map['session_id'] = $session_id;
			$map['open_id_pre'] = $open_id_pre;
			$map['is_ok'] = 0;
			$rs = D('session')->where($map)->find();
			if(!empty($rs)){				
				//写入落地域名的session_id
				//只能修改一次
				$session_data['show_session_id'] = session_id();
				$session_data['is_ok'] = 1;
				D('session')->where($map)->save($session_data);
				//echo D()->getLastSql();die;
				return ['result'=>1,'data'=>$rs];
			}else{
                return ['result'=>0];
            }
		}else{
		    //只有session_id的时候，要通过当前落地域名去获得用户的open_id_pre，以此来验证session是否是当前平台的信息
			//先获得当前平台的open_id_pre
			//现获得绑定域名
			$domain_rs = D('domain')->field('set_domain')->where("show_domain='".$_SERVER[HTTP_HOST]."'")->find();
			//获得open_id_pre
			$platform_rs = D('platform')->field('open_id_pre')->where("domain='".$domain_rs["set_domain"]."'")->find();
			
			//匹配登录信息是否合法
			$map['open_id_pre'] = $platform_rs['open_id_pre'];
			$map['show_session_id'] = session_id();
			$rs = D('session')->field('nickname,img,open_id,sex,domain,addtime,open_id_pre')->where($map)->find();
			if(!empty($rs) && $rs['addtime'] > time()){
				return ['result'=>1,'data'=>$rs];
			}
			else
				return ['result'=>0];
		}
	}	
}